package com.chenjiahao.kaiyanwan.ui.discovery

import android.os.Bundle
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chenjiahao.kaiyanwan.R
import com.chenjiahao.kaiyanwan.base.BaseFragment
import com.chenjiahao.kaiyanwan.logic.exception.ExceptionHandle
import kotlinx.android.synthetic.main.layout_recyclerview.*
import java.lang.Exception

/**
 * Created by chenjh on 2020/9/17
 */
class FollowFragment : BaseFragment() {
    private var mTitle: String? = null
    private val viewModel by lazy { ViewModelProvider(this).get(DiscoveryViewModel::class.java) }
    private val mFollowAdapter by lazy { activity?.let{ FollowAdapter(it,viewModel.followItemList) }}
    private var loadingMore=false

    /**
     * 伴生对象
     */
    companion object {
        fun getInstance(title: String): FollowFragment {
            val fragment = FollowFragment()
            val bundle = Bundle()
            fragment.arguments = bundle
            fragment.mTitle = title
            return fragment
        }
    }

    override fun getLayoutId() = R.layout.layout_recyclerview

    override fun initView() {
        mLayoutStatusView = multipleStatusView
        mRecyclerView.layoutManager = LinearLayoutManager(activity)
        mRecyclerView.adapter = mFollowAdapter
        mRecyclerView.addOnScrollListener(object :RecyclerView.OnScrollListener(){
            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                super.onScrollStateChanged(recyclerView, newState)
                val itemCount = (mRecyclerView.layoutManager as LinearLayoutManager).itemCount
                val lastVisibleItem =
                    (mRecyclerView.layoutManager as LinearLayoutManager).findLastVisibleItemPosition()
                if (!loadingMore && lastVisibleItem == (itemCount - 1)) {
                    loadingMore = true
                    //加载更多
                    viewModel.requestMoreData()
                }
            }
        })
    }

    override fun lazyLoad() {
        viewModel.requestFollow()
        viewModel.getFollowInfoData.observe(this, Observer {
            val followData=it.getOrNull()
            if(followData!=null){
                loadingMore=false
                viewModel.followNextPageUrl=followData.nextPageUrl
                viewModel.followItemList=followData.itemList
                mFollowAdapter?.addData(viewModel.followItemList)
            }else{
                showError(ExceptionHandle.handleException(it.exceptionOrNull() as Exception), ExceptionHandle.errorCode)
            }
        })
        viewModel.getIssueData.observe(this, Observer {
            val followMoreData=it.getOrNull()
            if(followMoreData!=null){
                loadingMore=false
                viewModel.followNextPageUrl=followMoreData.nextPageUrl
                viewModel.followItemList=followMoreData.itemList
                mFollowAdapter?.addData(viewModel.followItemList)
            }else{
                showError(ExceptionHandle.handleException(it.exceptionOrNull() as Exception), ExceptionHandle.errorCode)
            }
        })
    }
}